package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.service.AlertruleService;
import com.zzyl.vo.AlertRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description AlertruleController
 * @Author Lwj
 * @Date 2024-09-14-15:39
 */
@RestController
@RequestMapping("/alert-rule")
@Api(tags = "报警规则")
@Slf4j
public class AlertruleController extends BaseController {
    @Autowired
    private AlertruleService alertruleService;

    /**
     * 新增报警规则
     *
     * @param alertRuleDto
     * @return
     */
    @PostMapping("/create")
    @ApiOperation(value = "新增报警规则")
    public ResponseResult creatEalertRule(@RequestBody AlertRuleDto alertRuleDto) {
        logger.info("新增报警规则{}", alertRuleDto);
        alertruleService.creatEalertRule(alertRuleDto);
        return ResponseResult.success();
    }

    /**
     * 分页获取告警规则列表
     *
     * @param pageNum
     * @param pageSize
     * @param alertRuleName
     * @param functionName
     * @param productKey
     * @return
     */
    @GetMapping("/get-page")
    @ApiOperation(value = "分页获取告警规则列表")
    public ResponseResult<PageResponse<AlertRuleVo>> getPageAlertrule(
            @ApiParam(value = "页码") @RequestParam(value = "pageNum", required = true) Long pageNum,
            @ApiParam(value = "每页大小") @RequestParam(value = "pageSize", required = true) Long pageSize,
            @ApiParam(value = "规则名称，精确查询") @RequestParam(value = "alertRuleName", required = false) String alertRuleName,
            @ApiParam(value = "功能名称，精确查询") @RequestParam(value = "functionName", required = false) String functionName,
            @ApiParam(value = "产品ID") @RequestParam(value = "productKey", required = false) String productKey
    ) {
        log.info("分页获取告警规则列表!");
        PageResponse<AlertRuleVo> alertRuleVoPageResponse = alertruleService.getPageAlertrule(pageNum, pageSize, alertRuleName, functionName, productKey);
        return ResponseResult.success(alertRuleVoPageResponse);
    }

    /**
     * 获取单个告警规则
     *
     * @param id
     * @return
     */
    @GetMapping("/read/{id}")
    @ApiOperation(value = "获取单个告警规则")
    public ResponseResult<AlertRuleVo> getAlertruleById(@PathVariable(value = "id", required = true) Long id) {
        log.info("获取单个告警规则{}", id);
        AlertRuleVo alertRuleVo = alertruleService.getAlertruleById(id);
        return success(alertRuleVo);
    }

    /**
     * 更新告警规则
     *
     * @param alertRuleDto
     * @return
     */
    @PutMapping("/update/{id}")
    @ApiOperation(value = "更新告警规则")
    public ResponseResult updateAlertruleById(@RequestBody AlertRuleDto alertRuleDto) {
        log.info("更新告警规则{}", alertRuleDto);
        alertruleService.updateAlertruleById(alertRuleDto);
        return ResponseResult.success();
    }

    /**
     * 删除告警规则
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除告警规则")
    public ResponseResult deleteAlertruleById(@PathVariable(value = "id", required = true) Long id) {
        log.info("删除告警规则{}", id);
        alertruleService.deleteAlertruleById(id);
        return ResponseResult.success();
    }

    /**
     * 警告规则启用-禁用(0 禁用 1启用)
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/{id}/status/{status}")
    @ApiOperation(value = "警告规则启用-禁用(0 禁用 1启用)")
    public ResponseResult setStartAlertrule(
            @ApiParam(value = "警告规则id") @PathVariable(value = "id", required = true) Long id,
            @ApiParam(value = "警告规则状态(0 禁用 1启用)") @PathVariable(value = "status", required = true) Integer status) {
        log.info("警告规则{}启用-禁用(0 禁用 1启用){}", id, status);
        alertruleService.setStartAlertrule(id, status);
        return ResponseResult.success();
    }
}
